﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports CoreAudio
Imports System.Runtime.InteropServices
Public Class frmAudioControl
    Private device As MMDevice

    <DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As UInteger, ByVal lParam As UInteger) As UInteger
    End Function

    Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        If VistaControls.OSSupport.IsCompositionEnabled = True Then
            MyBase.OnPaint(e)
            e.Graphics.FillRectangles(Brushes.Black, New Rectangle() { _
                New Rectangle(0, 0, Me.ClientSize.Width, 0), _
                New Rectangle(Me.ClientSize.Width - 30, 0, 0, Me.ClientSize.Height), _
                New Rectangle(0, Me.ClientSize.Height - 30, Me.ClientSize.Width, 30)})
        End If
    End Sub

    Private Sub frmEAC_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Me.TopMost = True
        If VistaControls.OsSupport.IsCompositionEnabled = False Then
            lblPCA.Visible = True
            vlblCA.Visible = False
        Else
            VistaControls.Dwm.DwmManager.EnableGlassFrame(Me, New VistaControls.Dwm.Margins(0, 0, 0, 30))
        End If
        If Not frmMain.WindowState = FormWindowState.Minimized Then
            Dim Yform As Integer
            Dim Xform As Integer
            Yform = frmMain.Location.Y + frmMain.Height + 10
            Xform = frmMain.Location.X + 5
            Me.Location = New System.Drawing.Point(Xform, Yform)

        Else
            Me.StartPosition = FormStartPosition.CenterScreen
        End If
        Dim DevEnum As New MMDeviceEnumerator()
        device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
        tbMaster.Value = device.AudioEndpointVolume.MasterVolumeLevelScalar * 100
        AddHandler device.AudioEndpointVolume.OnVolumeNotification, AddressOf AudioEndpointVolume_OnVolumeNotification
        timer1.Enabled = True
        Me.TopMost = False
        If My.Settings.EacOnTop = True Then
            Me.TopMost = True
        End If
    End Sub

    Private Sub AudioEndpointVolume_OnVolumeNotification(ByVal data As AudioVolumeNotificationData)
        If Me.InvokeRequired Then
            Dim Params As Object() = New Object(0) {}
            Params(0) = data
            Me.Invoke(New AudioEndpointVolumeNotificationDelegate(AddressOf AudioEndpointVolume_OnVolumeNotification), Params)
        Else
            tbMaster.Value = data.MasterVolume * 100
        End If
    End Sub

    Private Sub tbMaster_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles tbMaster.Scroll
        device.AudioEndpointVolume.MasterVolumeLevelScalar = tbMaster.Value / 100.0
    End Sub

    Private Sub timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles timer1.Tick
        pkMaster.Value = MasterPeak()
        pkLeft.Value = LeftPeak()
        pkRight.Value = RightPeak()

        If Not frmMain.WindowState = FormWindowState.Minimized And My.Settings.EACStick = True Then
            Dim Yform As Integer
            Dim Xform As Integer
            Yform = frmMain.Location.Y + frmMain.Height + 10
            Xform = frmMain.Location.X + 5
            Me.Location = New System.Drawing.Point(Xform, Yform)
        End If
    End Sub

    Private Sub frmEAC_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        If My.Application.CommandLineArgs.Contains("/eac") Then
            frmMain.Close()
        End If
    End Sub

    Public Function MasterPeak() As Integer
        Dim Input As Integer = device.AudioMeterInformation.MasterPeakValue * 100
        If Input >= 100 Then
            Input = 100
        End If
        Return Input
    End Function

    Public Function LeftPeak() As Integer
        Dim Input As Integer = device.AudioMeterInformation.PeakValues(0) * 100
        If Input >= 100 Then
            Input = 100
        End If
        Return Input
    End Function

    Public Function RightPeak() As Integer
        Dim Input As Integer = device.AudioMeterInformation.PeakValues(1) * 100
        If Input >= 100 Then
            Input = 100
        End If
        Return Input
    End Function
End Class